package com.syl.upimg.service.impl;

import com.syl.upimg.bean.AliBean;
import com.syl.upimg.enums.ImageUploadTypeEnum;
import com.syl.upimg.service.BaseUpload;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;

/**
 * 薅阿里羊毛 缺点较慢
 *
 * @author syl
 * @create 2019-05-05 18:12
 **/
@Service
public class AliFleece implements BaseUpload {

    @Autowired
    private RestTemplate restTemplate;

    @Override
    public RestTemplate createRestTemplate() {
        return restTemplate;
    }

    @Override
    public String[] getLimit() {
        return new String[]{"jpg", "png", "gif"};
    }

    @Override
    public String getUrl() {
        return "https://kfupload.alibaba.com/mupload";
    }

    @Override
    public LinkedMultiValueMap<String, Object> getParam(FileSystemResource file, String fileName, String fileType) {
        LinkedMultiValueMap<String, Object> multiValueMap = new LinkedMultiValueMap<>();
        multiValueMap.add("file", file);
        multiValueMap.add("name", file.getFilename());
        multiValueMap.add("scene", "aeMessageCenterV2ImageRule");
        return multiValueMap;
    }

    public AliBean upload(String filePath) throws IOException {
        return this.upload(filePath, false);
    }

    public AliBean upload(String filePath, boolean delete) throws IOException {
        AliBean upload = upload(filePath, AliBean.class, delete);
        upload
            .setBaseUrl(upload.getUrl())
            .setImageUploadTypeEnum(ImageUploadTypeEnum.ALI)
        ;
        return upload;
    }

}
